﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

public partial class Manager_Nguoidung_Controls_UC_Chitietnguoidung : System.Web.UI.UserControl
{
    BUS_Accounts accounts = new BUS_Accounts();
    BUS_Customers customer = new BUS_Customers();
    BUS_Province province = new BUS_Province();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        
    }
    public void LoadData()
    {
        string id_ql = Request.QueryString["quanly"];
        string id_kh = Request.QueryString["khachhang"];
        DataTable dt = new DataTable();
        if (id_ql != null)
        {
            dt = accounts.Accounts_GetByTop(0, "Id = '" + id_ql + "'", "");    
        }
        else if (id_kh !=null)
        {
            dt = customer.Customers_GetByTop(0, "Id = '"+id_kh+"'", "");
        }
        else
        {
            Response.Redirect("Nguoidung.aspx");
        }
        
        txtUserName.Text = dt.Rows[0]["UserName"].ToString();
        txtFullName.Text = dt.Rows[0]["FullName"].ToString();
        txtBirthday.Text = string.Format("{0:d}",DateTime.Parse(dt.Rows[0]["Birthday"].ToString()));
        bool gt = bool.Parse(dt.Rows[0]["Sex"].ToString());
        if (gt)
        {
            rbNam.Checked = true;
        }
        else
        {
            rbNu.Checked = true;
        }
        txtDiachi.Text = dt.Rows[0]["Address"].ToString();
        txtMail.Text = dt.Rows[0]["Mail"].ToString();
        txtPhone.Text = dt.Rows[0]["Tel"].ToString();
        drlProvince.DataSource = province.Province_GetByAll();
       
        drlProvince.DataTextField = "Name";
        drlProvince.DataValueField = "Id";
        drlProvince.DataBind();

        drlProvince.SelectedValue = dt.Rows[0]["Province_Id"].ToString();
        bool trangthai = bool.Parse(dt.Rows[0]["Status"].ToString());
        if (trangthai)
        {
            rbMo.Checked = true;
        }
        else
        {
            rbDong.Checked = true;
        }

    }

    protected void btnCapnhat_Click(object sender, EventArgs e)
    {
        string id_ql = Request.QueryString["quanly"];
        string id_kh = Request.QueryString["khachhang"];
        
        if (id_ql != null)
        {
            DTO_Accounts objQL = new DTO_Accounts();
            objQL.UserName = txtUserName.Text;
            string pass = txtPassNew.Text;
            string passRe = txtPassNewRe.Text;
            
            objQL.FullName = txtFullName.Text;
            objQL.BirthDay = string.Format("{0:d}",DateTime.Parse(txtBirthday.Text));
            if (rbNam.Checked)
            {
                objQL.Sex = true;
            }
            else
            {
                objQL.Sex = false;
            }
            objQL.Province_Id = int.Parse(drlProvince.SelectedValue);
            objQL.Address = txtDiachi.Text;
            objQL.Tel = txtPhone.Text;
            objQL.Mail = txtMail.Text;
            if (rbMo.Checked)
            {
                objQL.Status = true;
            }
            else
            {
                objQL.Status = false;
            }
            if (pass != "" && passRe != "" && pass == passRe)
            {
                objQL.PassWord = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5");
                //Response.Redirect(Request.RawUrl);
                accounts.Accounts_UpdateByAll(id_ql, objQL);
                Response.Redirect(Request.RawUrl);
                
            }
            else if (pass != passRe)
            {
                Literal1.Text = "<p class='thongbao'>Lơi! Xác nhận mật khẩu không đúng</p>";
            }
            else
            {
                accounts.Accounts_UpdateByAll(id_ql, objQL);
                Response.Redirect(Request.RawUrl);
            }
           
            
            //DAL_Accounts acc = new DAL_Accounts();
            //Response.Write(acc.Accounts_UpdateByAll(id_ql,objQL));
            //Response.Redirect(Request.RawUrl);
        }
        else if (id_kh != null)
        {
            DTO_Customers objKH = new DTO_Customers();
            objKH.UserName = txtUserName.Text;
            string pass = txtPassNew.Text;
            string passRe = txtPassNewRe.Text;

            objKH.FullName = txtFullName.Text;
            objKH.BirthDay = string.Format("{0:d}", DateTime.Parse(txtBirthday.Text));
            if (rbNam.Checked)
            {
                objKH.Sex = true;
            }
            else
            {
                objKH.Sex = false;
            }
            objKH.Province_Id = int.Parse(drlProvince.SelectedValue);
            objKH.Address = txtDiachi.Text;
            objKH.Tel = txtPhone.Text;
            objKH.Mail = txtMail.Text;
            if (rbMo.Checked)
            {
                objKH.Status = true;
            }
            else
            {
                objKH.Status = false;
            }
            if (pass != "" && passRe != "" && pass == passRe)
            {
                objKH.PassWord = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5");
                //Response.Redirect(Request.RawUrl);
                customer.Customers_UpdateByAll(id_kh, objKH);
                Response.Redirect(Request.RawUrl);

            }
            else if (pass != passRe)
            {
                Literal1.Text = "<p class='thongbao'>Lỗi! Xác nhận mật khẩu không đúng</p>";
            }
            else
            {
                customer.Customers_UpdateByAll(id_kh, objKH);
                Response.Redirect(Request.RawUrl);
            }


            //DAL_Accounts acc = new DAL_Accounts();
            //Response.Write(acc.Accounts_UpdateByAll(id_ql,objQL));
            //Response.Redirect(Request.RawUrl);
        }
        
    }
}